package com.market.member.service.quick;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.sms.service.IQuickLoginService;
import com.block.sms.utils.JiGuangUtils;
import com.market.member.config.QuickLoginConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class JiGQuickLoginServiceImpl implements IQuickLoginService {

    static Map<String, String> errorCodeMap = new HashMap<>();
    static Map<String, String> notifyCodeMap = new HashMap<>();
    static {
        errorCodeMap.put("9000", "手机号验证一致");
        errorCodeMap.put("9003", "一键登录token过期，请重新登录");
        errorCodeMap.put("9005", "请稍后登录");//同一号码连续两次提交认证间隔过短
        errorCodeMap.put("9006", "操作频繁，请明天再试");
        errorCodeMap.put("9007", "操作频繁，请明天再试");

        notifyCodeMap.put("9018", "账户余额不足");
    }

    @Autowired
    QuickLoginConfig quickLoginConfig;

    @Override
    public String getMobile( String loginCode, String appCode) throws Exception {

        String appKey = quickLoginConfig.getZxAppKey();
        String appSecret = quickLoginConfig.getZxAppSecret();
        String privateKey = quickLoginConfig.getZxPrivateKey();
        if (StrUtil.equals("laiqian", appCode)){
            appKey = quickLoginConfig.getLqAppKey();
            appSecret = quickLoginConfig.getLqAppSecret();
            privateKey = quickLoginConfig.getLqPrivateKey();
        }else if (StrUtil.equals("kejie", appCode)){
            appKey = quickLoginConfig.getKjAppKey();
            appSecret = quickLoginConfig.getKjAppSecret();
            privateKey = quickLoginConfig.getKjPrivateKey();
        }else if (StrUtil.equals("meixin", appCode)){
            appKey = quickLoginConfig.getMxAppKey();
            appSecret = quickLoginConfig.getMxAppSecret();
            privateKey = quickLoginConfig.getMxPrivateKey();
        }

        String auth = HttpUtil.buildBasicAuth(appKey, appSecret, Charset.defaultCharset());
        String contentType = "application/json";

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("loginToken", loginCode);

        String response = HttpUtil.createPost(quickLoginConfig.getRequestUrl())
                .body(JSON.toJSONString(paramMap), contentType)
                .contentType(contentType)
                .auth(auth)
                .execute()
                .body();
        log.info("【极光一键登录】请求结束【response】{}【param】{}", response,  JSON.toJSONString(paramMap));

        if (response == null){
            return null;
        }
        JSONObject jsonObject = JSONObject.parseObject(response);
        String code = jsonObject.getString("code");

        if (!"8000".equals(code)){
            return null;
        }

        String phoneEn = jsonObject.getString("phone");
        String resultPhone = JiGuangUtils.decrypt(phoneEn, privateKey);

        return resultPhone;
    }

    /**
     * Tf6vRyzmcvRIeqnnPpjxQ4Bz8gzrqy4zJPqpL9ZAliLekdj9yMpE8RGTZ3HjgGYSueKOZW6fJIDnc0V8M3cimqV6k7MhrIN9A1d7yn6OzW2WwHQp/+6Zk0Y93XuUyQNjm49cSOES3c+EOCVIDWs8tBjg18C58IqB9zBWUXNBIHM=
     */
    public static void main(String[] args) throws Exception {

        String phoneEn = "Tf6vRyzmcvRIeqnnPpjxQ4Bz8gzrqy4zJPqpL9ZAliLekdj9yMpE8RGTZ3HjgGYSueKOZW6fJIDnc0V8M3cimqV6k7MhrIN9A1d7yn6OzW2WwHQp/+6Zk0Y93XuUyQNjm49cSOES3c+EOCVIDWs8tBjg18C58IqB9zBWUXNBIHM=";

        String priKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALktQoI40dmqJzcmnQf4QTZl31HocHoCibfWlcPO0/N5APl9HaYCS9T+f32KD3enlQf/YuRVqTng33N+517THiU7IFPEAEp43zur9p72LwgrRF5vkNugKRpYk+pviGv2dqBhSGkhZrdy/vRdSu4k9lgI9RI9ii79lV5oM7+PxsitAgMBAAECgYB5POS6lbDvLbQXK7TMLwGjN/eKjQB3IYpBTsEnPhcO5T56Gix7MC2t+iJGc0nFMXclkVOR43O0bUJfP8sEDn1BDMUdVQE9fEkLSBkjXLcVHxYWfxF7CvZRtulmqHE0CgnP6joM3fbez6oMSs0kL/AmWnM4tIDMQNxo1pjN+HgTEQJBAO4By+OohkjY8feHrbO0DcOwbSL1JEg9oYnk8CqJ5z1qUI+F8nTcOoJZuN45KgTFKNWSCw+7nUwBekWDoTWj9WMCQQDHLQc1GJSv5u6OuWFwJLgSHzu0FkxUf72+/FCUCTUEqk92DvrvDeh17Sls7GZINjTLIpLczNKMgum50Lb8MO6vAkEAtp/cQdh7CYSlzchnCX+8HA7Q/0TYzTG1NOjdO9bK/34HSMl/7BlcL88GokV8r2JVXeMM58qO/NYZayOAXHdtfwJAUdjUFGKUfzBDqczOGJpONcw16YwcM8EGhRwENMEP02n1G0uOnHu3ofYiRstiQJxd13fOqANXJjyJtTzqZPnqqwJBAIDtepkYYqEbqR8ApnE4dhhpM+Ruf1IYtrlLuhhmLk0GtTboR1+hIYyamefljM2kXprL8ujjkG+3G0Z4IECzJpk=";

        String resultPhone = JiGuangUtils.decrypt(phoneEn, priKey);

        System.out.println(resultPhone);
    }
}
